introduce clang-tidy to CI and integrate with codacy (#1071)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 25 Aug 2023 17:13:17 +0000 (11:13 -0600)
committerGitHub <noreply@github.com>
Fri, 25 Aug 2023 17:13:17 +0000 (11:13 -0600)
* add codacy clang-tidy to workflows

* attempt to run clang-tidy on ci

* update jammy image.

get rid of ppa
add clang-tidy, jq.

* fixes.

* whittle down tidy checks.

* kick out another tidy warning

.github/workflows/codacy-analysis.yaml [new file with mode: 0644]
tools/Dockerfile_jammy
tools/ci_run_tidy.sh [new file with mode: 0755]

diff --git a/.github/workflows/codacy-analysis.yaml b/.github/workflows/codacy-analysis.yaml
new file mode 100644 (file)
index 0000000..8ae2c2b
--- /dev/null
@@ -0,0 +1,47 @@
+name: Codacy clang-tidy
+
+on:
+  push:
+    branches: [ '**']
+  pull_request:
+    # The branches below must be a subset of the branches above
+    branches: [ master ]
+  workflow_dispatch: ~
+
+jobs:
+  ubuntu:
+    name: ubuntu Build
+    runs-on: ubuntu-latest
+    strategy:
+      fail-fast: false
+      matrix:
+        include:
+          - IMAGE: 'jammy'
+            CMAKE_PREFIX_PATH: '/usr/lib/x86_64-linux-gnu/cmake/Qt5'
+            SCRIPT: './tools/ci_run_tidy.sh'
+    container:
+      image: gpsbabel-docker.jfrog.io/tsteven4/gpsbabel_build_environment_${{ matrix.IMAGE }}
+      env:
+        LC_ALL: 'C.UTF-8'
+        JOB_CMAKE_PREFIX_PATH: ${{ matrix.CMAKE_PREFIX_PATH }}
+        JOB_SCRIPT: ${{ matrix.SCRIPT }}
+        JOB_CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
+
+    steps:
+    - name: Checkout repository
+      uses: actions/checkout@v3
+
+    - name: build_and_test
+      run: |
+        # when using containers manually whitelist the checkout directory to allow git commands to work
+        git config --global --add safe.directory "${GITHUB_WORKSPACE}"
+        if [ -n "${JOB_CMAKE_PREFIX_PATH}" ]; then
+          CMAKE_PREFIX_PATH="${JOB_CMAKE_PREFIX_PATH}"
+          export CMAKE_PREFIX_PATH
+        fi
+        if [ -n "${JOB_CODACY_PROJECT_TOKEN}" ]; then
+          CODACY_PROJECT_TOKEN="${JOB_CODACY_PROJECT_TOKEN}"
+          export CODACY_PROJECT_TOKEN
+        fi
+        "${JOB_SCRIPT}"
+
index f47f3a234556d124e8388320128169d9676dc844..2545c4acb125fb27911c7343269ba808fc9be591 100644 (file)
@@ -29,6 +29,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
     cmake \
     ninja-build \
     clazy \
+    clang-tidy \
+    jq \
+    curl \
  && rm -rf /var/lib/apt/lists/*
 
 # alternative compiler
diff --git a/tools/ci_run_tidy.sh b/tools/ci_run_tidy.sh
new file mode 100755 (executable)
index 0000000..8130438
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/bash -ex
+
+CODACY_URL="https://api.codacy.com"
+COMMIT=$(git log -1 --format='%H')
+CODACY_CLANG_TIDY=$(curl -s https://api.github.com/repos/codacy/codacy-clang-tidy/releases/latest | jq '.assets[] | select(.name|startswith("codacy-clang-tidy-linux-")) | .browser_download_url' | tr -d \")
+
+CHECKS="clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,bugprone-*,google-*,misc-*,performance-*,readability-*,-cppcoreguidelines-pro-type-vararg,-modernize-use-trailing-return-type,-readability-identifier-length"
+HEADERFILTER=".*"
+
+mkdir bld
+cd bld
+cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DGPSBABEL_ENABLE_PCH=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
+# generate included ui files
+cmake --build .
+cd ..
+
+# exclude third party library source
+jq '[.[]|select(.file|contains("zlib")|not)] | [.[]|select(.file|contains("shapelib")|not)] | [.[]|select(.file|contains("bld")|not)]' \
+bld/compile_commands.json \
+> compile_commands.json
+
+# run-clang-tidy may still be forcing injection of escape sequences for colors.
+# this will cause codacy-clang-tidy to not find anything.
+sed "s/, '--use-color'//" "$(which run-clang-tidy)" > run-clang-tidy-nocolor
+chmod +x run-clang-tidy-nocolor
+./run-clang-tidy-nocolor -p "$(pwd)" -header-filter "${HEADERFILTER}" -checks "${CHECKS}" | \
+tee tidy.out
+
+curl -L "${CODACY_CLANG_TIDY}" --output codacy-clang-tidy
+chmod +x codacy-clang-tidy
+
+
+./codacy-clang-tidy < tidy.out > tidy.report
+
+# don't leak secrets
+set +x
+curl -XPOST -L -H "project-token: $CODACY_PROJECT_TOKEN" \
+    -H "Content-type: application/json" -d @tidy.report \
+    "$CODACY_URL/2.0/commit/$COMMIT/issuesRemoteResults"
+
+curl -XPOST -L -H "project-token: $CODACY_PROJECT_TOKEN" \
+        -H "Content-type: application/json" \
+        "$CODACY_URL/2.0/commit/$COMMIT/resultsFinal"
+